package com.doeis.minaserver.module;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

import io.netty.bootstrap.ServerChannelFactory;
import io.netty.channel.ChannelException;
import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.ServerChannel;
import io.netty.util.internal.StringUtil;

public class NettyServerChannelFactory<T extends ServerChannel> implements ServerChannelFactory<T> {

	
     private List<ServerChannel> listServerChannel = new ArrayList<ServerChannel>();
	 private final Class<? extends T> clazz;

     NettyServerChannelFactory(Class<? extends T> clazz) {
         this.clazz = clazz;
     }

     @Override
     public T newChannel(EventLoop eventLoop, EventLoopGroup childGroup) {
         try {
             Constructor<? extends T> constructor = clazz.getConstructor(EventLoop.class, EventLoopGroup.class);
             T t = constructor.newInstance(eventLoop, childGroup);
             System.err.println(clazz+"  >>  "+t);
             listServerChannel.add(t);
             return t;
         } catch (Throwable t) {
             throw new ChannelException("Unable to create Channel from class " + clazz, t);
         }
     }
     
     public int getListServerChannelSize(){
    	 return listServerChannel.size();
     }

     public List<ServerChannel> getListServerChannel() {
		return listServerChannel;
	}

	@Override
     public String toString() {
         return StringUtil.simpleClassName(clazz) + ".class";
     }
}
